import java.util.Arrays;

public class MyArrayList {
    private int[] elem;
    private int usedSize;
    private static final int DEFAULT_SIZE = 10;

    public MyArrayList() {
        this.elem = new int[DEFAULT_SIZE];
    }

    public MyArrayList(int initCapacity) {
        this.elem = new int[initCapacity];
    }

    //打印顺序表
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.println(this.elem[i] + " ");
        }
    }

    //添加元素
    public boolean isFull() {
        if (this.usedSize == this.elem.length) {
            return true;
        }
        return false;
    }

    public void add(int data) {
        if (isFull() == true) {
            this.elem = Arrays.copyOf(this.elem, 2 * this.elem.length);
        }
        this.elem[this.usedSize] = data;
        this.usedSize++;
    }

    //新增元素
    public void add(int pos, int data) {
        if (pos < 0 || pos > this.elem.length) {
            System.out.println("位置不合法");
            return;
        }
        if (isFull() == true) {
            this.elem = Arrays.copyOf(this.elem, 2 * this.elem.length);
        }
        for (int i = this.usedSize - 1; i > 0; i--) {
            this.elem[i + 1] = this.elem[i];
        }
        this.elem[pos] = data;
        this.usedSize++;
    }

    //判断是否包含某个元素
    public boolean contains(int data) {
        for (int i = 0; i < this.usedSize; i++) {
            if (this.elem[i] == data) {
                return true;
            }
        }
        return false;
    }

    //查找某个元素的对应位置
    public int index(int data) {
        for (int i = 0; i < this.usedSize; i++) {
            if (data == this.elem[i]) {
                return i;
            }
        }
        return -1;
    }

    //获取pos位置处的元素
    public int get(int pos){
        if(pos < 0 || pos > this.usedSize){
            System.out.println("位置不合法");
            return -1;
        }
        return this.elem[pos];
    }

    //将pos处的元素设置为value
    public void checkPos(int pos, int value){
        if(pos < 0 || pos > this.usedSize){
            System.out.println("位置不合法");
            return;
        }
        this.elem[pos] = value;
    }

    //删除第一次出现的关键字key
    public void remove(int key){
        int  index = this.index(key);
        if(index == -1){
            System.out.println("没有这个数据");
            return;
        }
        for(int i = index; i < this.usedSize; i++){
            this.elem[i] = this.elem[i+1];
        }
        this.usedSize--;
    }

    //获取链表的长度
        public int size(){
        return this.usedSize;
    }
}